/**
 * 
 */
package com.huniu.links.hive;

import java.io.File;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.huniu.links.colony.ILeaderBeeBehavior;
import com.huniu.links.colony.LeaderBeeBehaviorImpl;
import com.huniu.utility.DateUtil;

/**
 * @author zandy (chenheng)
 *
 */
public class LeaderBeeBehaviorPoolImpl implements ILeaderBeeBehaviorPool {
	
	private List<File> linkFileList ;
	private String seedFolder ;
	
	public void setLinkFileList(List<File> linkFileList) {
		this.linkFileList = linkFileList ;
	}

	public void setSeedFolder(String seedFolder) {
		this.seedFolder = seedFolder;
	}

	public void execute() {
		String seedFolder = generateSeedFolder();
		ExecutorService executor = Executors.newCachedThreadPool();
		for ( File file : linkFileList ) {
			ILeaderBeeBehavior leaderBehavior = new LeaderBeeBehaviorImpl();
			leaderBehavior.setLinkFile(file);
			leaderBehavior.setSeedFolder(seedFolder);
			executor.execute(leaderBehavior);
		}
		executor.shutdown();
	}
	
	private String generateSeedFolder() {
		GregorianCalendar calender = new GregorianCalendar();
		Date currentDate = calender.getTime();
		String childFolder = DateUtil.convertDateToString(currentDate,
				DateUtil.DATE_FORMAT_1);
		String folder = seedFolder + File.separator + childFolder;
		File file = new File(folder);
		if (! file.exists()) {
			file.mkdirs();
		}
		return folder ;
	}

}
